package ee.ut.maxorato_macobo.oop.mud.items;

import java.util.Scanner;

import ee.ut.maxorato_macobo.oop.mud.game.DataParser;
import ee.ut.maxorato_macobo.oop.mud.game.GameUtils;
import ee.ut.maxorato_macobo.oop.mud.game.PlayerCharacter;

public class ItemBase {
	private static long globalItemIndexCounter = 1;
	
	private ItemTemplate itemTemplate;
	protected String modifier;
	protected ContainsItems itemLocation;
	protected boolean staticItem;
	protected String customDescription;
	protected long itemIndex;
	protected ItemLoader itemLoader;
	
	public ItemBase(ItemTemplate newItemTemplate, ContainsItems newItemLocation) {
		itemIndex = globalItemIndexCounter++;
		
		itemTemplate = newItemTemplate;
		itemLocation = null;
		modifier = "";
		
		itemLoader = null;
		
		customDescription = null;
		
		if(newItemLocation != null) {
			moveToLocation(newItemLocation);
		}
	}
	
	public void attachLoader(ItemLoader newItemLoader) {
		itemLoader = newItemLoader;
	}
	
	public void destroyItem() {
		if(itemLoader != null) {
			itemLoader.instanceDetached();
		}
		
		ItemProcessorPool.unregisterItem(this);
		
		removeFromLocation();
	}
	
	public long getItemIndex() {
		return itemIndex;
	}
	
	public void parseExtraSection(Scanner parser) {
		DataParser.parseDummy(parser);
	}
	
	public void describeForPlayer(PlayerCharacter player) {
		player.sendTickMessage(getLookString() + "\r\n" + getDescription());
	}
	
	@Override
	public ItemBase clone() {
		return new ItemBase(itemTemplate, itemLocation);
	}
	
	public ContainsItems getLocation() {
		return itemLocation;
	}
	
	public String getItemType() {
		return itemTemplate.getItemType();
	}
	
	public void removeFromLocation() {
		if(itemLocation != null) {
			itemLocation.itemRemove(this);
			
			itemLocation = null;
		}
		
		if(itemLoader != null) {
			itemLoader.instanceDetached();
			
			itemLoader = null;
		}
	}
	
	public void setStatic(boolean newStatic) {
		staticItem = newStatic;
	}
	
	public boolean isStatic() {
		return staticItem;
	}
	
	public void moveToLocation(ContainsItems newItemLocation) {
		if(newItemLocation != null && newItemLocation.canAddItem(this) != null) return;
		else if(this instanceof ContainsItems && (ContainsItems)this == newItemLocation) return;
		
		removeFromLocation();
		
		itemLocation = newItemLocation;
		
		if(itemLocation != null) {
			itemLocation.itemAdd(this);
		}
	}
	
	public int getWeight() {
		return itemTemplate.getWeight();
	}
	
	public String getName() {
		return itemTemplate.getName();
	}
	
	public String getLongName() {
		return itemTemplate.getLongName();
	}
	
	public void setCustomDescription(String newDescription) {
		customDescription = newDescription;
	}
	
	public String getDescription() {
		if(customDescription != null) return customDescription;
		else return itemTemplate.getDescription();
	}
	
	public ItemTemplate getTemplate() {
		return itemTemplate;
	}
	
	public String getModifier() {
		return modifier;
	}
	
	public String getLookString() {
		return GameUtils.stringCapitalize(getLongName()) + " is here on the ground.";
	}
	
	public String getShortLookString() {
		return getName();
	}
	
	public void processTick() {
		
	}
	
}
